<?php
require_once 'core/PluginLoader.php';
require_once 'core/PluginModel.php';

$pluginDir = __DIR__ . '/plugins';
$loader = new PluginLoader($pluginDir);
$pluginModel = new PluginModel();

// 获取分类
$categories = $pluginModel->getCategories();
$currentCategory = $_GET['category'] ?? 'all';
$searchKeyword = $_GET['search'] ?? '';

// 获取插件列表
if ($searchKeyword) {
    $plugins = $pluginModel->searchPlugins($searchKeyword);
} elseif ($currentCategory !== 'all') {
    $plugins = $pluginModel->getPluginsByCategory($currentCategory);
} else {
    $plugins = $pluginModel->getActivePlugins();
}

// 获取统计数据
$stats = $pluginModel->getStats();
$topPlugins = $pluginModel->getTopPlugins(6);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>幻影工具网 - 在线工具集合</title>
    <meta name="description" content="免费在线工具集合，包含网页源码查看、HTTP状态查询、JSON格式化、图片转Base64、加密解密、时间戳转换等多种实用工具">
    <meta name="keywords" content="在线工具,网页工具,JSON格式化,Base64转换,加密解密,时间戳,二维码生成">
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#6366F1'
                    }
                }
            }
        }
    </script>
</head>
<body class="bg-gradient-to-br from-blue-50 to-indigo-50 min-h-screen flex flex-col">
    <!-- 顶部导航 -->
    <nav class="bg-white/80 backdrop-blur-sm shadow-sm border-b border-blue-100 sticky top-0 z-50">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between items-center h-16">
                <div class="flex items-center">
                    <h1 class="text-2xl font-bold bg-gradient-to-r from-blue-600 to-indigo-600 bg-clip-text text-transparent">
                        幻影工具网
                    </h1>
                </div>
                <div class="flex items-center space-x-4">
                    <a href="/admin" class="text-gray-600 hover:text-blue-600 transition-colors">后台管理</a>
                </div>
            </div>
        </div>
    </nav>

    <div class="flex-1">
        <!-- 搜索栏 -->
        <div class="bg-white/60 backdrop-blur-sm border-b border-blue-100">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
                <form method="GET" class="max-w-2xl mx-auto">
                    <div class="relative">
                        <input type="text" name="search" value="<?php echo htmlspecialchars($searchKeyword); ?>" 
                               placeholder="搜索工具..." 
                               class="w-full px-4 py-3 pl-12 rounded-xl border border-blue-200 focus:border-blue-400 focus:ring-2 focus:ring-blue-200 transition-all">
                        <svg class="absolute left-4 top-1/2 transform -translate-y-1/2 w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
                        </svg>
                    </div>
                </form>
            </div>
        </div>

        <!-- 分类标签 -->
        <?php if (empty($searchKeyword) && !empty($categories)): ?>
        <div class="bg-white/60 backdrop-blur-sm border-b border-blue-100">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4">
                <div class="flex flex-wrap gap-2">
                    <a href="?category=all" 
                       class="px-4 py-2 rounded-lg text-sm font-medium transition-all <?php echo $currentCategory === 'all' ? 'bg-blue-500 text-white shadow-lg' : 'bg-white/80 text-gray-700 hover:bg-blue-50 hover:text-blue-600'; ?>">
                        全部
                    </a>
                    <?php foreach ($categories as $category): ?>
                    <a href="?category=<?php echo $category['id']; ?>" 
                       class="px-4 py-2 rounded-lg text-sm font-medium transition-all <?php echo $currentCategory == $category['id'] ? 'bg-blue-500 text-white shadow-lg' : 'bg-white/80 text-gray-700 hover:bg-blue-50 hover:text-blue-600'; ?>">
                        <?php echo htmlspecialchars($category['name']); ?>
                    </a>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- 热门工具 -->
        <?php if (empty($searchKeyword) && $currentCategory === 'all' && !empty($topPlugins)): ?>
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
            <h2 class="text-xl font-bold text-gray-800 mb-6 flex items-center">
                <svg class="w-5 h-5 mr-2 text-red-500" fill="currentColor" viewBox="0 0 20 20">
                    <path fill-rule="evenodd" d="M12.395 2.553a1 1 0 00-1.45-.385c-.345.23-.614.558-.822.88-.214.33-.403.713-.57 1.116-.334.804-.614 1.768-.84 2.734a31.365 31.365 0 00-.613 3.58 2.64 2.64 0 01-.945-1.067c-.328-.68-.398-1.534-.398-2.654A1 1 0 005.05 6.05 6.981 6.981 0 003 11a7 7 0 1011.95-4.95c-.592-.591-.98-.985-1.348-1.467-.363-.476-.724-1.063-1.207-2.03zM12.12 15.12A3 3 0 017 13s.879.5 2.5.5c0-1 .5-4 1.25-4.5.5 1 .786 1.293 1.371 1.879A2.99 2.99 0 0113 13a2.99 2.99 0 01-.879 2.121z" clip-rule="evenodd"></path>
                </svg>
                热门工具
            </h2>
            <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
                <?php foreach ($topPlugins as $plugin): ?>
                <a href="plugin.php?name=<?php echo urlencode($plugin['name']); ?>" 
                   class="group bg-white/80 backdrop-blur-sm rounded-2xl p-6 shadow-sm hover:shadow-xl transition-all duration-300 border border-blue-100 hover:border-blue-300">
                    <div class="flex items-start justify-between mb-4">
                        <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-blue-500 to-indigo-600 flex items-center justify-center text-white font-bold text-lg">
                            <?php echo strtoupper(substr($plugin['title'], 0, 1)); ?>
                        </div>
                        <div class="flex items-center space-x-2 text-sm text-gray-500">
                            <span class="flex items-center">
                                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>
                                </svg>
                                <?php echo number_format($plugin['view_count']); ?>
                            </span>
                            <span class="flex items-center">
                                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"></path>
                                </svg>
                                <?php echo number_format($plugin['like_count']); ?>
                            </span>
                        </div>
                    </div>
                    <h3 class="text-lg font-bold text-gray-800 mb-2 group-hover:text-blue-600 transition-colors">
                        <?php echo htmlspecialchars($plugin['title']); ?>
                    </h3>
                    <p class="text-gray-600 text-sm leading-relaxed">
                        <?php echo htmlspecialchars($plugin['description']); ?>
                    </p>
                </a>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- 工具列表 -->
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
            <?php if ($searchKeyword): ?>
            <h2 class="text-xl font-bold text-gray-800 mb-6">
                搜索 "<?php echo htmlspecialchars($searchKeyword); ?>" 的结果 (<?php echo count($plugins); ?>)
            </h2>
            <?php elseif ($currentCategory !== 'all'): ?>
            <h2 class="text-xl font-bold text-gray-800 mb-6">
                <?php 
                $categoryName = '未知分类';
                foreach ($categories as $cat) {
                    if ($cat['id'] == $currentCategory) {
                        $categoryName = $cat['name'];
                        break;
                    }
                }
                echo htmlspecialchars($categoryName);
                ?> (<?php echo count($plugins); ?>)
            </h2>
            <?php else: ?>
            <h2 class="text-xl font-bold text-gray-800 mb-6">所有工具 (<?php echo count($plugins); ?>)</h2>
            <?php endif; ?>

            <?php if (empty($plugins)): ?>
            <div class="text-center py-12">
                <svg class="w-16 h-16 mx-auto text-gray-400 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.172 16.172a4 4 0 015.656 0M9 12h6m-6-4h6m2 5.291A7.962 7.962 0 0112 15c-2.34 0-4.47-.881-6.08-2.33"></path>
                </svg>
                <p class="text-gray-500 text-lg">暂无工具</p>
            </div>
            <?php else: ?>
            <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
                <?php foreach ($plugins as $plugin): ?>
                <a href="plugin.php?name=<?php echo urlencode($plugin['name']); ?>" 
                   class="group bg-white/80 backdrop-blur-sm rounded-2xl p-6 shadow-sm hover:shadow-xl transition-all duration-300 border border-blue-100 hover:border-blue-300">
                    <div class="flex items-start justify-between mb-4">
                        <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-blue-500 to-indigo-600 flex items-center justify-center text-white font-bold text-lg">
                            <?php echo strtoupper(substr($plugin['title'], 0, 1)); ?>
                        </div>
                        <div class="flex items-center space-x-2 text-sm text-gray-500">
                            <span class="flex items-center">
                                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>
                                </svg>
                                <?php echo number_format($plugin['view_count']); ?>
                            </span>
                            <span class="flex items-center">
                                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"></path>
                                </svg>
                                <?php echo number_format($plugin['like_count']); ?>
                            </span>
                        </div>
                    </div>
                    <h3 class="text-lg font-bold text-gray-800 mb-2 group-hover:text-blue-600 transition-colors">
                        <?php echo htmlspecialchars($plugin['title']); ?>
                    </h3>
                    <p class="text-gray-600 text-sm leading-relaxed">
                        <?php echo htmlspecialchars($plugin['description']); ?>
                    </p>
                </a>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <?php include 'foot.php'; ?>
</body>
</html> 